package org.dromara.basic.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.basic.domain.bo.MemberBo;
import org.dromara.basic.domain.bo.TeachClassBo;
import org.dromara.basic.domain.vo.MemberVo;
import org.dromara.basic.domain.vo.TeachClassVo;
import org.dromara.basic.service.IMemberService;
import org.dromara.basic.service.ITeachClassService;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.run.service.IDeptService;
import org.dromara.run.service.IUserService;

import java.util.List;

@Slf4j
public class TeachClassListener extends AnalysisEventListener<TeachClassVo> implements ExcelListener<TeachClassVo> {

    private final IDeptService deptService;

    private final ITeachClassService teachClassService;
    private final Boolean isUpdateSupport;
    private final IUserService userService;
    private final Long operUserId;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public TeachClassListener(Boolean isUpdateSupport) {
        this.deptService = SpringUtils.getBean(IDeptService.class);
        this.userService = SpringUtils.getBean(IUserService.class);
        this.teachClassService = SpringUtils.getBean(ITeachClassService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.operUserId = LoginHelper.getUserId();
    }

    @Override
    public ExcelResult<TeachClassVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，用户详细信息导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，用户详细信息数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<TeachClassVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }

    @Override
    public void invoke(TeachClassVo teachClassVo, AnalysisContext analysisContext) {
        TeachClassVo teachClass = this.teachClassService.queryByClassName(teachClassVo.getGrade()+teachClassVo.getDeptName());
        String deptId = this.deptService.selectDeptIdByDeptName(teachClassVo.getDeptName());
        String teacherId = this.userService.selectUserIdByName(teachClassVo.getTeacherName());
        try {
            //验证是否有这个读者
            if (ObjectUtil.isNull(teachClass)&& StringUtils.isNotEmpty(deptId)) {
                TeachClassBo teachClassBo = BeanUtil.toBean(teachClassVo, TeachClassBo.class);
                ValidatorUtils.validate(teachClassBo);
                teachClassBo.setCreateBy(operUserId);
                teachClassBo.setDeptId(deptId);
                if (StringUtils.isNotEmpty(teacherId)) {
                    teachClassBo.setTeacherId(teacherId);
                }
                teachClassService.insertByB(teachClassBo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、班级信息 ").append(teachClassBo.getClassName()+teachClassVo.getDeptName()).append(" 导入成功");
            } else if (isUpdateSupport&& StringUtils.isNotEmpty(deptId)) {
                Long stuId = teachClass.getClassId();
                TeachClassBo teachClassBo = BeanUtil.toBean(teachClassVo, TeachClassBo.class);
                teachClassBo.setClassId(stuId);
                ValidatorUtils.validate(teachClassBo);
                teachClassBo.setDeptId(deptId);
                teachClassBo.setUpdateBy(operUserId);
                if (StringUtils.isNotEmpty(teacherId)) {
                    teachClassBo.setTeacherId(teacherId);
                }
                teachClassService.updateByBo(teachClassBo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、班级信息 ").append(teachClassBo.getGrade()+teachClassVo.getDeptName()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、班级信息 ").append(teachClassVo.getGrade()+teachClassVo.getDeptName()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、班级信息 " + teachClassVo.getGrade()+teachClassVo.getDeptName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
